/**
 * 版权所有:版权所有(C) 2020，卓信科技
 * 系统名称:FOL2020-财务在线
 * 文件名称:SysDictServiceImpl.java
 * 模块名称:SysDictServiceImpl
 * 创建日期:2020年12月07日 下午04:36:07
 * 作        者:wangyihan
 * 内容摘要:字典表
 */
package com.zx.emanage.sysmanage.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zx.emanage.sysmanage.persist.SysDictMapper;
import com.zx.emanage.sysmanage.service.ISysDictService;
import com.zx.emanage.util.gen.entity.SysDict;


/**
 * 类  名  称:SysDictServiceImpl
 * 内容摘要:字典表
 * 作        者:wangyihan
 */
@Service("sysDictService")
public class SysDictServiceImpl implements ISysDictService
{
    @Autowired
    private SysDictMapper sysDictMapper;
	

    @Override
    public List<SysDict> getDictAll(Map<String, Object> map)
    {
        return sysDictMapper.getDictAll(map);
    }

    @Override
    public Integer getCount(Map<String, Object> map)
    {
        return sysDictMapper.getDictCount(map);
    }

    @Override
    public Object getDictCount(Map<String, Object> search)
    {
        return sysDictMapper.getDictCount(search);
    }

    @Override
    public Map<String, Object> selectAllDicts()
    {
        List<Map<String,Object>> listMap=sysDictMapper.selectAllDicts();
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < listMap.size(); i++)
        {
            if("0".equals(listMap.get(i).get("p_sys_dict_id").toString())) {
                for (int j = 0; j < listMap.size(); j++)
                {
                    String sys_dict_id=listMap.get(i).get("sys_dict_id").toString();
                    String p_sys_dict_id= listMap.get(j).get("p_sys_dict_id").toString();
                    if(sys_dict_id.equals(p_sys_dict_id)) {
                        map.put(listMap.get(i).get("key_code").toString()+"_"+listMap.get(j).get("key_code").toString()+"", listMap.get(j).get("value_meaning").toString()+"");
                    }
                    
                }
                
            }
            
        }
        
        return map;
    }

    @Override
    public String saveDictData(SysDict sysdict)
    {
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("key_code", sysdict.getKey_code());
        map.put("p_sys_dict_id", sysdict.getP_sys_dict_id());
        int insertResult = 0;
        if (sysdict.getDict_level() == 1 || (sysdict.getDict_level() == 2 && sysDictMapper.getDictCountForIn(map) == 0))
        {
            insertResult = sysDictMapper.saveDictData(sysdict);
        }
        else
        {
            return "key_codeRepeat";
        }
        
        if (insertResult > 0)
        {
            return "success";
        }else {
            return "fail";
        }
    }

    @Override
    public Map<String, Object> getByDictId(String sys_dict_id)
    {
        return sysDictMapper.getByDictId(sys_dict_id);
    }

    @Override
    public String updateDictData(SysDict sysdict)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("key_code", sysdict.getKey_code());
        map.put("sys_dict_id", sysdict.getSys_dict_id());
        map.put("p_sys_dict_id", sysdict.getP_sys_dict_id());

        int insertResult = 0;
        if (sysdict.getDict_level() == 1 || (sysdict.getDict_level() == 2 && sysDictMapper.getDictCountForIn(map) == 0))
        {
            insertResult = sysDictMapper.updateDictData(sysdict);
        }
        else
        {
            return "key_codeRepeat";
        }

        if (insertResult > 0)
        {
            return "success";
        }
        else
        {
            return "fail";
        }
    }

    @Override
    public String delDictData(String idList)
    {
        String delDictArray[] = idList.split(",");
        for (int i = 0; i < delDictArray.length; i++)
        {
            sysDictMapper.delDictData(delDictArray[i]);
        }
        return "success";
    }

    @Override
    public List<SysDict> getByParentDictId(String p_sys_dict_id)
    {
        return sysDictMapper.getByParentDictId(p_sys_dict_id);
    }

    @Override
    public List<Map<String, Object>> getDictListByKey_code(String key_code)
    {
        return sysDictMapper.getDictListByKey_code(key_code);
    }

    @Override
    public Map<String, Object> getSelectsList()
    {
        //获取字典表所有数据
        List<Map<String, Object>> listMap = sysDictMapper.selectAllDicts();
        //返回总的结果
        Map<String, Object> result = new HashMap<String, Object>();
        for (int i = 0; i < listMap.size(); i++)
        {
            
            if ("0".equals(listMap.get(i).get("p_sys_dict_id").toString()))
            {
                //每个大select下的options
                List<Map<String, Object>> optionsList = new ArrayList<Map<String, Object>>();

                for (int j = 0; j < listMap.size(); j++)
                {
                    if (listMap.get(i).get("sys_dict_id").toString().equals(listMap.get(j).get("p_sys_dict_id").toString()))
                    {
                        optionsList.add(listMap.get(j));
                    }
                }
                result.put(listMap.get(i).get("key_code").toString(), optionsList);

            }
            
        }
        return result;
    }

}
